package com.bm.servlet;

import com.bm.order.IOrderService;
import com.bm.order.OrderService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Eirik Luka n7696698
 */
public class Quote extends HttpServlet {
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Order</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Order at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
        OrderService service = new OrderService();
        IOrderService port = service.getBasicHttpBindingIOrderService();
        
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String email = request.getParameter("email");
        String vehicleSize = request.getParameter("vehicleSize");
        String[] parts = request.getParameterValues("parts");
        
        
        String name = firstName + " " + lastName;
        
//        Quote o = new Quote();
//        
//        o.setName(name);
//        o.setEmail(email);
//        o.setParts(parts);

        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Order</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Order</h1>");
            out.println("Name: " +  firstName + " " + lastName + "<br />");
            out.println("<br />");
            out.println("Email:" + email + "<br />");
            out.println("<br />");
            out.println("Vehicle size: " + vehicleSize + "<br />");
            out.println("<br />");
            out.println("Parts: <br />");
            for(int i = 0; i < parts.length; i++) {
                out.println("<p style=\"margin: 0px; padding-left: 20px;\">" + parts[i] + "</p>");
            }
            out.println("</body>");
            out.println("</html>");
            
            
            
        } finally {            
            out.close();
        }
    }
}